package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.NotificationDto;
import com.tcm.entity.dto.param.NotificationPageParam;
import com.tcm.entity.vo.NotificationListResult;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.NotificationService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/28 16:53
 */
@RestController
@RequestMapping("/pc/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 添加通知
     * @param dto 通知数据封装类
     * @param request http请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addNotification(@RequestBody @Valid NotificationDto dto, HttpServletRequest request){
        boolean b = false;
        try {
            b = notificationService.addNotification(dto, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_BLANK);
    }

    /**
     * 删除通知
     * @param id 通知ID
     * @param request http请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeNotification(@RequestParam Long id, HttpServletRequest request){
        boolean b = false;
        try {
            b = notificationService.removeNotification(id, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 分页获取通知列表
     * @param param 通知参数封装
     * @return 通知列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getNotificationListByPage(NotificationPageParam param){
        PageR<NotificationListResult> notificationListByPage = notificationService.getNotificationListByPage(param);
        return R.success(notificationListByPage);
    }

}
